package com.dindang.maintenance.mvp.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.maintenance.R;
import com.dindang.maintenance.mvp.model.entity.Maintain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxw on & 2018/11/8
 */
public class SelectDateDialog extends Dialog {

    public SelectDateDialog(@NonNull Context context) {
        super(context);
    }

    public SelectDateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected SelectDateDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private OnClickListener listener;
        private String title;
        List<Maintain.SelectDate> list = new ArrayList<>();
        private SelectDateAdapter adapter;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            if("".equals(title)){
                this.title = "请设置标题";
            }else{
                this.title = title;
            }
            return this;
        }

        public Builder setListener(OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_title;
        private ImageView iv_dialog_close;
        private RecyclerView rv_date;

        private SelectDateDialog dialog;

        public SelectDateDialog create() {
            dialog = new SelectDateDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.maintenance_dialog_select_date, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_title = view.findViewById(R.id.maintenance_tv_title);
            iv_dialog_close = view.findViewById(R.id.maintenance_iv_dialog_close);
            rv_date = view.findViewById(R.id.maintenance_rv_date);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            iv_dialog_close.setOnClickListener(v -> dismiss());
            adapter = new SelectDateAdapter();
            rv_date.setAdapter(adapter);
            return dialog;
        }

        public void dismiss(){
            if(dialog != null){
                dialog.dismiss();
            }
        }

        public void show(){
            dialog.show();
        }

        public void setSelectDate(List<Maintain.SelectDate> list) {
            this.list = list;
            adapter.notifyDataSetChanged();

            Maintain.SelectDate mSelectDate = null;
            for (Maintain.SelectDate selectDate : this.list) {
                if(selectDate.isSelect){
                    mSelectDate = selectDate;
                }
            }
            int position = this.list.indexOf(mSelectDate);
            if (position != -1) {
                rv_date.scrollToPosition(position);
                LinearLayoutManager mLayoutManager = (LinearLayoutManager) rv_date.getLayoutManager();
                mLayoutManager.scrollToPositionWithOffset(position, 0);
            }

        }

        class SelectDateAdapter extends RecyclerView.Adapter<SelectDateAdapter.ViewHolder> {

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.maintenance_item_date, parent, false);
                SelectDateAdapter.ViewHolder viewHolder = new SelectDateAdapter.ViewHolder(view);
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                Maintain.SelectDate selectDate = list.get(position);
                holder.tv_date.setText(selectDate.dateString);

                if(selectDate.isSelect){
                    holder.iv_select.setVisibility(View.VISIBLE);
                }else{
                    holder.iv_select.setVisibility(View.GONE);
                }

                holder.ll_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (Maintain.SelectDate s : list) {
                            s.isSelect = false;
                        }
                        selectDate.isSelect = true;
                        adapter.notifyDataSetChanged();
                        listener.onClick(position);
                    }
                });
            }

            @Override
            public int getItemCount() {
                return list.size();
            }


            class ViewHolder extends RecyclerView.ViewHolder {
                LinearLayout ll_item;
                TextView tv_date;
                ImageView iv_select;
                ViewHolder(View itemView) {
                    super(itemView);
                    ll_item = itemView.findViewById(R.id.maintenance_ll_item);
                    tv_date = itemView.findViewById(R.id.maintenance_tv_date);
                    iv_select = itemView.findViewById(R.id.maintenance_iv_select);
                }
            }
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
    }

    public interface OnClickListener {
        void onClick(int position);
    }

}
